/**
 * Autoloader Extend
 * @author Yoshiki Kozaki(www.joomler.net)
 */

/**
 * SyntaxHighlighter
 * http://alexgorbatchev.com/SyntaxHighlighter
 *
 * SyntaxHighlighter is donationware. If you are using it, please donate.
 * http://alexgorbatchev.com/SyntaxHighlighter/donate.html
 *
 * @version
 * 3.0.83 (July 02 2010)
 * 
 * @copyright
 * Copyright (C) 2004-2010 Alex Gorbatchev.
 *
 * @license
 * Dual licensed under the MIT and GPL licenses.
 */
(function() {

var sh = SyntaxHighlighter;

/**
 * Provides functionality to dynamically load only the brushes that a needed to render the current page.
 *
 * There are two syntaxes that autoload understands. For example:
 * 
 * SyntaxHighlighter.autoloader(
 *     [ 'applescript',          'Scripts/shBrushAppleScript.js' ],
 *     [ 'actionscript3', 'as3', 'Scripts/shBrushAS3.js' ]
 * );
 *
 * or a more easily comprehendable one:
 *
 * SyntaxHighlighter.autoloader(
 *     'applescript       Scripts/shBrushAppleScript.js',
 *     'actionscript3 as3 Scripts/shBrushAS3.js'
 * );
 */
sh.autoloader = function(options, path)
{
	var elements = sh.findElements(), brushes, scripts=[], load, i;
		
	function getAliases()
	{
		var s = {}, t={}, i, v;
		
		t['shBrushAppleScript.js'] = ['applescript'];
		t['shBrushAS3.js'] = ['actionscript3', 'as3'];
		t['shBrushBash.js'] = ['bash', 'shell'];
		t['shBrushColdFusion.js'] = ['coldfusion', 'cf'];
		t['shBrushCpp.js'] = ['cpp', 'c'];
		t['shBrushCSharp.js'] = ['c#', 'c-sharp', 'csharp'];
		t['shBrushCss.js'] = ['css'];
		t['shBrushDelphi.js'] = ['delphi', 'pascal'];
		t['shBrushDiff.js'] = ['diff', 'patch', 'pas'];
		t['shBrushErlang.js'] = ['erl', 'erlang'];
		t['shBrushGroovy.js'] = ['groovy'];
		t['shBrushJava.js'] = ['java'];
		t['shBrushJavaFX.js'] = ['jfx', 'javafx'];
		t['shBrushJScript.js'] = ['js', 'jscript', 'javascript'];
		t['shBrushPerl.js'] = ['perl', 'pl'];
		t['shBrushPhp.js'] = ['php'];
		t['shBrushPlain.js'] = ['text', 'plain'];
		t['shBrushPython.js'] = ['py', 'python'];
		t['shBrushRuby.js'] = ['ruby', 'rails', 'ror', 'rb'];
		t['shBrushSass.js'] = ['sass', 'scss'];
		t['shBrushScala.js'] = ['scala'];
		t['shBrushSql.js'] = ['sql'];
		t['shBrushVb.js'] = ['vb', 'vbnet'];
		t['shBrushXml.js'] = ['xml', 'xhtml', 'xslt', 'html'];
		for(i in t){
			for(v = 0; v < t[i].length; v++)
				s[t[i][v]] = i;
		}
		
		return s;
	}
	
	for(i in options) sh.defaults[i] = options[i]; 
	
	brushes = getAliases();
	// dynamically add <script /> tags to the document body
	for (i = 0; i < elements.length; i++)
	{	
		loadScript(brushes[elements[i].params.brush]);
	}
	
	function loadScript(url)
	{
		if(!url) return;
		var script = document.createElement('script');
		script.src = path+url;
		script.type = 'text/javascript';
		script.language = 'javascript';
		script.onload = script.onreadystatechange = function()
		{
			if (!scripts[url] && (!this.readyState || this.readyState == 'loaded' || this.readyState == 'complete'))
			{
				scripts[url] = true;
				
				// Handle memory leak in IE
				script.onload = script.onreadystatechange = null;
				script.parentNode.removeChild(script);
			}
		};
		
		// sync way of adding script tags to the page
		document.body.appendChild(script);
		load = true;
	};
	
	if(!scripts['shBrushXml.js'] && sh.defaults['html-script']) loadScript('shBrushXml.js');
	
	if(load) sh.all();
};

})();
